import messageUtil from "../utils/messageUtil";

/**
 * 普通支付
 */
const payHandler = (tradeNo, successCallBack, failCallBack) => {
	uni.getProvider({
		service: 'payment',
		success: res => {
			console.log('getProvider success', res, tradeNo);
			console.log(res.provider[0]);
			uni.requestPayment({
				provider: res.provider[0],
				orderInfo: tradeNo,
				success: (successRes) => {
					// {"result":"","resultCode":"6001","callbackUrl":"","memo":"支付未完成","extendInfo":{},"errMsg":"requestPayment:ok"}
					console.log('requestPayment success:' + JSON.stringify(successRes));
					if (successRes.resultCode == 6001) {
						messageUtil.closeLoading()
						messageUtil.showFail('支付失败')
						return;
					}
					if (successCallBack) {
						successCallBack(successRes)
					}

				},
				fail: (err) => {
					console.log('requestPayment fail:' + JSON.stringify(err));
					if (failCallBack) {
						failCallBack(err)
					}
				}
			})
		},
		fail: res => {
			console.log('getProvider fail', res);
		}
	})
}

/**
 * 周期扣款
 */
const zhouqiPayHandler = (signStr, successCallBack, failCallBack) => {
	my.paySignCenter({
		signStr: signStr,
		success: (res) => {
			console.log('zhouqiPayHandler success ' + JSON.stringify(res))
			console.log(res.resultStatus);
			if (res.resultStatus && parseInt(res.resultStatus) == 6001) {
				messageUtil.closeLoading()
				messageUtil.showFail('支付失败')
				return;
			}
			if (successCallBack) {
				successCallBack(res)
			}
		},
		fail: (err) => {
			console.log('zhouqiPayHandler fail ' + JSON.stringify(err))
			if (failCallBack) {
				failCallBack(err)
			}
		}
	});
}

export default {
	payHandler,
	zhouqiPayHandler,
}